package com.ebt.m.customer.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ebt.m.R;
import com.ebt.m.customer.entity.Attachment;

import java.util.List;

/**
 * Created by archer.qi on 2016/12/8.
 */
public class PicAdapter extends BaseAdapter {

    private Context context;
    private List<Attachment> mAttachments;
    private LayoutInflater inflater;

    // 是否可以添加
    private boolean shouldAdd;

    public PicAdapter(Context c, List<Attachment> data, boolean shouldAdd) {
        context = c;
        mAttachments = data;
        this.shouldAdd = shouldAdd;
        inflater = LayoutInflater.from(c);
    }

    public void setData(List<Attachment> data) {
        mAttachments = data;
    }

    /* 是否点击了'添加' */
    public boolean isPositionForAdd(int pos) {
        if (pos == getCount() - 1) {
            return true;
        }
        return false;
    }

    @Override
    public int getCount() {
        if (shouldAdd) {
            return mAttachments == null ? 1 : mAttachments.size() + 1;
        }
        return mAttachments == null ? 0 : mAttachments.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        PicViewHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_grid_pics, null);
            holder = new PicViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (PicViewHolder) view.getTag();
        }

        if (i == length()) {
            // 添加
            holder.img.setImageResource(R.drawable.selector_plus_bounded);
        } else {
            // 显示
            holder.loadImage(mAttachments.get(i));
        }

        return view;
    }

    private int length() {
        return mAttachments == null ? 0 : mAttachments.size();
    }

    private class PicViewHolder {
        private ImageView img;

        public PicViewHolder(View view) {
            img = (ImageView) view.findViewById(R.id.gridItem_pic_add);
        }

        public void loadImage(Attachment at) {
            if (at != null) {
                if (!TextUtils.isEmpty(at.getPath())) {
                    display(at.getPath());
                } else {
                    if (!TextUtils.isEmpty(at.getUrl())) {
                        display(at.getUrl());
                    }
                }
            }
        }

        public void display(String addr) {
            Glide.with(context)
                    .load(addr)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(img);
        }
    }
}
